﻿//-----------------------------------------------------------------------
// <copyright file="TaskSolveAttempt.cs" company="Numbrella foundation">
// Copyright (c) Numbrella foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Numbrella.Core
{
	using System;
	using System.Diagnostics;

	/// <summary>
	/// Represents description of the attempt of solving task of a problem.
	/// </summary>
	/// <typeparam name="TResultContents">Type of the task result contents.</typeparam>
	internal class TaskSolveAttempt<TResultContents>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="TaskSolveAttempt{TResultContents}"/> class.
		/// </summary>
		/// <param name="userName">Login of the task solver.</param>
		/// <param name="startTime">Time when task solving was started.</param>
		public TaskSolveAttempt(string userName, DateTime startTime)
		{
			Debug.Assert(!String.IsNullOrEmpty(userName), "User name can not be empty");
			
			this.UserName = userName;
			this.StartTime = startTime;
		}

		/// <summary>
		/// Gets login of the task solver.
		/// </summary>
		public string UserName { get; private set; }

		/// <summary>
		/// Gets time when task solving was started.
		/// </summary>
		public DateTime StartTime { get; private set; }

		/// <summary>
		/// Gets task result received from user associated with current solve attempt.
		/// </summary>
		public TaskResult<TResultContents> ReceivedResult { get; private set; }

        /// <summary>
		/// Checks if given timeout for task solver was exceeded.
		/// </summary>
		/// <param name="timeout">Timeout to check.</param>
		/// <returns>True if timeout was exceeded; otherwise, false.</returns>
		public bool IsTimeoutExceeded(TimeSpan timeout)
		{
			return DateTime.Now - this.StartTime >= timeout;
		}

		/// <summary>
		/// Receives task result.
		/// </summary>
		/// <param name="result">Task result.</param>
		public void ReceiveResult(TaskResult<TResultContents> result)
		{
			Debug.Assert(result != null, "Result can ot be null.");
			Debug.Assert(this.ReceivedResult == null, "Result should not be received before.");

			this.ReceivedResult = result;
		}
	}
}
